

import _ from "lodash";
import { Component, Vue, Prop } from "vue-property-decorator";
import { ipcRenderer } from "electron";
//import { MysqlError, FieldInfo } from "mysql";
//import { Field } from "ant-design-vue/types/form/form";

const CONN_KEY = "sqlite-test-01";
const FILE_KEY = "data/sqlite_t.db";

@Component
export default class Sqlite_test extends Vue {
    /** 标签页信息 */
    public tabList = [
        { key: "input", tab: "输入" },
        { key: "result", tab: "结果" }
    ];

    /** 当前标签页 */
    public tabKey = "input";

    public statement: string = "select now();"; // "select * from users;";
    /** 查询结果的字段数据源 */
    public sql_source_field: (string[] | null)[] = [];
    /** 查询结果的数据源 */
    public sql_source_result: any[][] = [];
    /** 当前结果的索引 */
    public sql_current_index: number = 0;

    get sql_columns(): Object[] {
        let fields = this.sql_source_field[this.sql_current_index];
        //let results = this.sql_source_result[this.sql_current_index];
        if (_.isNull(fields)) {
            return _.map(["key", "value"], x => ({
                title: x,
                dataIndex: x
            }));
        } else {
            return _.map(fields, f => ({
                title: f,
                dataIndex: f
            }));
        }
    }

    get sql_result(): Object[] {
        let ret = [];
        let fields = this.sql_source_field[this.sql_current_index];
        let results = this.sql_source_result[this.sql_current_index];
        if (_.isNull(fields)) {
            let obj = results[0];
            return _.map(_.toPairs(obj), ([key, value]) => ({ key, value }));
        }
        else {
            return results;
        }
    }

    public result_scroll_top: number = 0;

    async created() {
        let ret_key = await ipcRenderer.sendSync("sqlite-conn", {
            db_key: CONN_KEY,
            file: FILE_KEY
        });
        console.log(ret_key);
    }

    onTabChange(key: string) {
        this.tabKey = key;
    }

    async sqlQuery() {
        console.log("开始查询：" + new Date().toISOString());

        let {
            status,
            message,
            error,
            rows
        }: {
            status: string,
            message: string,
            error: Error,
            rows: Object[]
        } = await ipcRenderer.sendSync("sqlite-all", {
            db_key: CONN_KEY,
            statement: this.statement
        });

        // /*
        console.log({ status, message, error, rows });
        // */

        if (status == "success") {
            // 判断是否为复式结果 ???

            if (rows.length > 0) {
                let keys = _.keys(rows[0]);
                this.sql_source_field = [keys];
                this.sql_source_result = [rows];
            }
            else {
                this.sql_source_field = [[]];
                this.sql_source_result = [[]];
            }
        } else {
            this.sql_source_field = [null];
            this.sql_source_result = [[{ status, message, ...error }]];
        }
        this.sql_current_index = 0;
        this.tabKey = "result";
    }

    async sqlRun() {
        console.log("开始处理：" + new Date().toISOString());

        let {
            status,
            message,
            error
        }: {
            status: string,
            message: string,
            error: Error
        } = await ipcRenderer.sendSync("sqlite-run", {
            db_key: CONN_KEY,
            statement: this.statement
        });

        console.log({ status, message, error });

        this.sql_source_field = [null];
        this.sql_source_result = [[{ status, message, ...error }]];
        this.sql_current_index = 0;
        this.tabKey = "result";

    }

    resultScroll(event: Event) {
        if (event.target != null) {
            this.result_scroll_top = _.get(event.target, "scrollTop", 0);
        } else {
            this.result_scroll_top = 0;
        }
    }
}